package com.atguigu.tingshu.account.consumer;

import com.atguigu.tingshu.account.service.MqOpsService;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;


@Component
public class UserAccountReceiver {


    @Autowired
    private MqOpsService mqOpsService;


    /**
     * 消费消费
     * 对于队列 交换机 以及绑定关系可以通过配置来指定 也可以通过下游rabbitlistern注解指定
     */

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConst.QUEUE_USER_REGISTER, durable = "true", exclusive = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ACCOUNT),
            key = MqConst.ROUTING_USER_REGISTER))
    @SneakyThrows   // 可以绕开编译期间的异常 等运行时发现异常才抛出
    public void listenUserAccountInit(String content, Message message, Channel channel) {

        // 1.获取消息，进行判断
        if (StringUtils.isEmpty(content)) {
            return;
        }

        // 2.消费消息
        mqOpsService.userAccountInit(content);

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        // 3.手动应答
        channel.basicAck(deliveryTag,false);


    }


}